home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl / 5.10.0 / Text / Tabs.pm < prev    next >
Encoding:
Perl POD Document  |  2009-06-26  |  1.3 KB  |  90 lines

  1. package Text::Tabs;
  2.  
  3. require Exporter;
  4.  
  5. @ISA = (Exporter);
  6. @EXPORT = qw(expand unexpand $tabstop);
  7.  
  8. use vars qw($VERSION $tabstop $debug);
  9. $VERSION = 2007.1117;
  10.  
  11. use strict;
  12.  
  13. BEGIN    {
  14.     $tabstop = 8;
  15.     $debug = 0;
  16. }
  17.  
  18. sub expand {
  19.     my @l;
  20.     my $pad;
  21.     for ( @_ ) {
  22.         my $s = '';
  23.         for (split(/^/m, $_, -1)) {
  24.             my $offs = 0;
  25.             s{\t}{
  26.                 $pad = $tabstop - (pos() + $offs) % $tabstop;
  27.                 $offs += $pad - 1;
  28.                 " " x $pad;
  29.             }eg;
  30.             $s .= $_;
  31.         }
  32.         push(@l, $s);
  33.     }
  34.     return @l if wantarray;
  35.     return $l[0];
  36. }
  37.  
  38. sub unexpand
  39. {
  40.     my (@l) = @_;
  41.     my @e;
  42.     my $x;
  43.     my $line;
  44.     my @lines;
  45.     my $lastbit;
  46.     my $ts_as_space = " "x$tabstop;
  47.     for $x (@l) {
  48.         @lines = split("\n", $x, -1);
  49.         for $line (@lines) {
  50.             $line = expand($line);
  51.             @e = split(/(.{$tabstop})/,$line,-1);
  52.             $lastbit = pop(@e);
  53.             $lastbit = '' 
  54.                 unless defined $lastbit;
  55.             $lastbit = "\t"
  56.                 if $lastbit eq $ts_as_space;
  57.             for $_ (@e) {
  58.                 if ($debug) {
  59.                     my $x = $_;
  60.                     $x =~ s/\t/^I\t/gs;
  61.                     print "sub on '$x'\n";
  62.                 }
  63.                 s/  +$/\t/;
  64.             }
  65.             $line = join('',@e, $lastbit);
  66.         }
  67.         $x = join("\n", @lines);
  68.     }
  69.     return @l if wantarray;
  70.     return $l[0];
  71. }
  72.  
  73. 1;
  74. __END__
  75.  
  76. sub expand
  77. {
  78.     my (@l) = @_;
  79.     for $_ (@l) {
  80.         1 while s/(^|\n)([^\t\n]*)(\t+)/
  81.             $1. $2 . (" " x 
  82.                 ($tabstop * length($3)
  83.                 - (length($2) % $tabstop)))
  84.             /sex;
  85.     }
  86.     return @l if wantarray;
  87.     return $l[0];
  88. }
  89.  
  90.